package com.zhuoyue.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SourceDownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/x-download");
        response.setHeader("Content-Disposition","attachment; filename=你好.jpg");
        OutputStream out = response.getOutputStream();//获取输出流

        InputStream in = new FileInputStream("d:/1.jpg");
        int i = in.read();
        while(i != -1){
            out.write(i);
            i = in.read();
        }
        System.out.println("文件下载完成");
    }
}
